package jsonTest;

import java.io.IOException;
import java.util.List;

import json.Json_Account;
import json.Json_City;
import json.Json_Company;
import json.Json_Product;
import json.Json_Result;
import jsonTest.JsonUtil;

import handler.Handler;
import handler.Message;

import org.json.JSONException;

import util.LogUtil;

import com.ruiyi.baseparser.Group;
import com.ruiyi.error.RuiyiCredentialsException;
import com.ruiyi.error.RuiyiError;
import com.ruiyi.error.RuiyiException;
import com.ruiyi.error.RuiyiParseException;


public class _Test2 {
    private static final String TAG = "_Test2\t\t";
    
    static Json_Account ac;
    public static void main(String[] argz) {
    	testMyCompany();
    }

	final static MyCompany company = new MyCompany();		
	final static Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case 0:
				LogUtil.e(TAG, "get Account success, to get my company.");
				getCompany(company);
				break;
			case 1:
				LogUtil.e(TAG, "get my company success.");
				company.setCompany((Json_Company)msg.obj);

				getProductList(company);
				break;
			case 2:
				LogUtil.e(TAG, "products not null.");
				break;
			}
		}
	};
	private static void testMyCompany() {

		company.login("locoso@locoso.com", "locoso", new MyCompany.OnDataReady(){
			@Override
			public void setData(Object obj) {
				Json_Account ac;
				if(obj == null){
					LogUtil.e(TAG, "obj null");
					return;
				}
				if(obj instanceof Json_Account){
					ac = (Json_Account)obj;
					LogUtil.e(TAG, "obj not null.\tsession:" + ac.getSession());
					company.setAccount(ac);
					handler.sendMessage(Message.Obtain(handler, 0, null));
					return;
				}
				if(obj instanceof Json_Result){
					LogUtil.e(TAG, "obj not null.\tmessage:" + ((Json_Result)obj).getMessage());
					return;
				}
			}
		});
	}

	private static void getCompany(final MyCompany company) {
		company.getMyCompany(company.getAccount(), new MyCompany.OnDataReady(){
			@Override
			public void setData(Object obj) {
				Json_Company comp;
				if(obj == null){
					LogUtil.e(TAG, "obj null");
					return;
				}
				if(obj instanceof Json_Company){
					comp = (Json_Company)obj;
					LogUtil.e(TAG, "obj not null.\tgetCompanyName:" + comp.getCompanyName());
					handler.sendMessage(Message.Obtain(handler, 1, comp));
					return;
				}
				if(obj instanceof Json_Result){
					LogUtil.e(TAG, "obj not null.\tmessage:" + ((Json_Result)obj).getMessage());	
					return;
				}			
			}
		});		
	}

	protected static void getProductList(MyCompany company) {
		company.getProductList(company.getCompany(), new MyCompany.OnDataReady(){
			@Override
			public void setData(Object obj) {
				List<Json_Product> products;
				if(obj == null){
					LogUtil.e(TAG, "products null");
					return;
				}
				if(obj instanceof List<?>){
					products = (List<Json_Product> )obj;
					handler.sendMessage(Message.Obtain(handler, 2, products));
					return;
				}
				if(obj instanceof Json_Result){
					LogUtil.e(TAG, "result not null.\tmessage:" + ((Json_Result)obj).getMessage());	
					return;
				}		
			}
		});
	}


	//*********************************************************************************************
	private static void testProductList() {
		JsonUtil util = new JsonUtil();
		try {
			Group<Json_Product> products = util.getProductList(4303825);
			for(Json_Product tmp : products)
				LogUtil.d(TAG, tmp.getProductName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void testLogin(){
		Mgr.getInstance().login("locoso@locoso.com", "locoso", new Mgr.OnDataReady(){
			@Override
			public void setData(Object obj) {
				if(obj == null){
					LogUtil.e(TAG, "obj null");
					return;
				}
				if(obj instanceof Json_Account){
					ac = (Json_Account)obj;
					LogUtil.e(TAG, "obj not null.\tsession:" + ac.getSession());	
					return;
				}
				if(obj instanceof Json_Result){
					LogUtil.e(TAG, "obj not null.\tmessage:" + ((Json_Result)obj).getMessage());	
					return;
				}			
			}
		});
	}
}
